package com.supermap.mongodb.controller;

import java.io.IOException;

import javax.servlet.MultipartConfigElement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.supermap.mongodb.service.FileService;
import com.supermap.mongodb.tool.MogDbFile;
import com.supermap.tool.FhData;

/**
 * 
* @ClassName: FileController 
* @Description: monggodb文件操作
* @author yuyao
* @date 2018年1月8日 下午2:23:26
 */
@Controller
public class FileController {

    @Autowired
    private FileService fileService;
    
    /**
     * 获取文件片信息
     * @param id
     * @return
     */
    @GetMapping("files/{id}")
    @ResponseBody
    public FhData serveFile(@PathVariable String id) {
    	FhData data= new FhData();
    	data.setCod(0);
    	data.setMsg("Success!");
    	data.setData(fileService.getFileById(id));
        return data;
    }
    
    /**
     * 在线显示文件
     * @param id
     * @return
     */
    @GetMapping("/view/{id}")
    @ResponseBody
    public FhData serveFileOnline(@PathVariable String id) {
    	FhData data= new FhData();
    	data.setCod(0);
    	data.setMsg("Success!");
    	data.setData(fileService.getFileById(id));
        return data;
        
    }
 
    /**
     * 上传接口
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public FhData handleFileUpload(Object obj,MultipartFile file) {
       
    	FhData data= new FhData();
    	
    	try {
        	MogDbFile f = new MogDbFile(file.getOriginalFilename(),  file.getContentType(), file.getSize(),file.getBytes());
        	
        	if(file.getSize()<2*1024L * 1024L) {
        		 fileService.saveFile(f);
        	}else {
        		 fileService.saveGridFile(file);
			}
        	data.setCod(0);
         	data.setMsg("Success!");
        	return data;
 
        } catch (IOException  ex) {
            data.setCod(1);
         	data.setMsg(ex.getMessage());
        	return data;
        }
    }
    
    /**
     * 删除文件
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    @ResponseBody
    public FhData deleteFile(@PathVariable String id) {
 
    	FhData data= new FhData();
    	try {
			fileService.removeFile(id);
			data.setCod(0);
	    	data.setMsg("DELETE Success!");
			return data;
		} catch (Exception e) {
			data.setCod(0);
	    	data.setMsg(e.getMessage());
			return data;
		}
    }
    
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize(500*1024L * 1024L);
        return factory.createMultipartConfig();
    }
}
